$(document).ready(function () {
			  var vars = [], hash;
			  var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('#');
			      
				    for(var i = 0; i < hashes.length; i++) {
				        hash = hashes[i].split('=');
				        vars.push(hash[0]);
				        vars[hash[0]] = hash[1];
				    }
					  var email=vars["user"];
					  var userName;
					  
					  Parse.$= jQuery;
					  	 Parse.initialize("UCyMTCIXJqmFk3oLy4CwkxXv6oymztzqqoQNWAux", "BBYPhajAwBgLFzDTXvnx4MaReUn8Kke2KkwQ4yTT");
					     //Parse.initialize("gLxhWhhBK7SPLZ7Jevs6q2lcLBlhje3l3sQlyS0e", "nPvJsuTXpFeKvNyDjZg8ks8sYwt2nntVOi9hr91J");
					     
					    var UserRegister = Parse.Object.extend("Contact");
						var query = new Parse.Query(UserRegister);
						query.equalTo("email", email);
						query.find({
					  		success: function(results) {	
							      var contact = results[0];
							      userName = contact.get("name");
					  
					  
								  // Get a reference to the root of the chat data.
								  var chatRef = new Firebase("https://wayf.firebaseio.com/share/");
								  var auth = new FirebaseSimpleLogin(chatRef, function(error, user) {
									  if (!error) {
									    // Success!
									  }
									});
									auth.login('persona', {
								  		rememberMe: true
									});
								  // When the user presses enter on the message input, write the message to firebase.
								  $('#messageInput').keypress(function (e) {
								    if (e.keyCode == 13) {
								      var name = userName;
								      var title = $('#messageTitle').val();
								      var text = $('#messageInput').val();
								      chatRef.push({name:name, text:text, title:title});
								      $('#messageInput').val('');
								      $('#messageTitle').val('');
								    }
								  });
								chatRef.on('child_added', function(snapshot) {
							        var message = snapshot.val();
							        displayChatMessage(message.name, message.text, message.title);
							      });
							      function displayChatMessage(name, text, title) {
							        $('<div/>').text(text).prepend($('<em/>').text(title+'!! '+name+': ')).appendTo($('#messagesDiv'));
							        $('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;
								  };
							},
								 error: function(results, error) {
						            alert("Error: " + error.code + " " + error.message);
						          }
					});
});